/*
 * Atomic Predicates for Transformers
 * 
 * Copyright (c) 2015 UNIVERSITY OF TEXAS AUSTIN. All rights reserved. Developed
 * by: HONGKUN YANG and SIMON S. LAM http://www.cs.utexas.edu/users/lam/NRL/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * with the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimers.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimers in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the UNIVERSITY OF TEXAS AUSTIN nor the names of the
 * developers may be used to endorse or promote products derived from this
 * Software without specific prior written permission.
 * 
 * 4. Any report or paper describing results derived from using any part of this
 * Software must cite the following publication of the developers: Hongkun Yang
 * and Simon S. Lam, Scalable Verification of Networks With Packet Transformers
 * Using Atomic Predicates, IEEE/ACM Transactions on Networking, October 2017,
 * Volume 25, No. 5, pages 2900-2915 (first published as IEEE Early Access
 * Article, July 2017, Digital Object Identifier: 10.1109/TNET.2017.2720172).
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
 * THE SOFTWARE.
 */
package transformer;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;

import common.PositionTuple;

public class TestNATCOVEAP3 extends NATTester{

    String net_name;

    public TestNATCOVEAP3(String outputfolder, String outputfilename,
            int longest_run, int w, double t, String net_name) {
        super(outputfolder, outputfilename, longest_run, w, t);

        this.net_name = net_name;
    }

    @Override
    public double one_run(int nat_number) {
        NetworkCOVEAP cove = null;


        String serialized = net_name + "ap.ser";
        try{

            //cove = new NetworkCOVEAP(net_name, null);
            
            if(new File(serialized).isFile()) 
            {
                cove = NetworkCOVEAP.read_object(serialized);
            }else
            {
                //String edgerouterfile = networkname+"edge";
                cove = new NetworkCOVEAP(net_name, null);
                cove.save_object();
            }
            
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        cove.add_nat_ap(nat_number, true);


        HashSet<PositionTuple> allpts = cove.getallactiveports();
        HashSet<PositionTuple> usedpts = new HashSet<PositionTuple> ();

        for (PositionTuple pt : allpts)
        {
            String box_name = pt.getPortName();
            /*
              if(!visited_boxes.contains(box_name))
              {
                    usedpts.add(pt);
                    visited_boxes.add(box_name);
              }*/
            if(box_name.equals(net_name))
            {
                usedpts.add(pt);
            }
        }
        System.out.println("Ports " + usedpts+" selected");

        return StateTransferAP.compute_trees_anypkt(cove, usedpts, null);

    }

    public static void main(String[] args) throws IOException
    {
        // nat_i2/
        String outputfolder = args[0];
        // result
        String outputfilename = args[1];
        // 100
        int longest_run = Integer.parseInt(args[2]);
        // 5
        int w_size = Integer.parseInt(args[3]);
        // 0.005
        double thres = Double.parseDouble(args[4]);
        //25568 - test example
        String net_name = args[5];

        TestNATCOVEAP3 tncap = new TestNATCOVEAP3(outputfolder, outputfilename, longest_run, w_size, thres, net_name);

        // 
        int num_nat = Integer.parseInt(args[6]);
        tncap.add_nat_allrouters(num_nat);


    }

}
